<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sys_brand_manage extends CI_Controller {


	public function __construct() {
		session_start();
		parent::__construct();
	}

	public function  index(){
		$this->brand_list();
	}
	public function brand_list(){


		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();


		$this->load->library("common/right_manage");
		$data['update_brand']	=false;
		if ($this->right_manage->check_right("update_brand")) {
			$data['update_brand']	=true;
		}
		$data['delete_brand']	=false;
		if ($this->right_manage->check_right("delete_brand")) {
			$data['delete_brand']=true;
		}
		$data['add_brand']	=false;
		if ($this->right_manage->check_right("add_brand")) {
			$data['add_brand']	=true;
		}
		$data['query_brand']	=false;
		if ($this->right_manage->check_right("query_brand")) {
			$data['query_brand']	=true;
		}



		$this->load->library("sys/brand");
		$data['brand_list'] 	=$this->brand->query_brand_list();
		$data['num']			=$this->brand->query_brand_num();



		$this->load->library("tools/pagination");
		$data['page_link']=$this->pagination->get_pagination(base_url()."index.php/sys_brand_manage/brand_list?",$data['num']);

		$this->load->view("sys/brand/brand_list",$data);
	}
	public function add_brand(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_brand",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->view("sys/brand/add_brand",$data);
	}
	public function save_brand(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_brand",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}

		$brand['brand_name'] =trim($this->input->post("brand_name"));
		$brand['create_by']		=$_SESSION['user_name'];
		$brand['create_time']	=date("Y-m-d H:i:s");
		$brand['modify_by']		=$_SESSION['user_name'];
		$brand['modify_time']	=date("Y-m-d H:i:s");
		$this->load->library("sys/brand");
		$result =$this->brand->save_brand($brand);
		$this->load->helper("common");
		echo JSON($result);
	}
	public function edit_brand(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_brand",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$brand_id =$this->input->get("brand_id");
		$this->load->library("sys/brand");
		$data['brand_info']=$this->brand->query_brand_info($brand_id);

		$this->load->view("sys/brand/edit_brand",$data);
	}

	public function update_brand(){

		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_brand",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}

		$brand_id =$this->input->post('brand_id');
		$brand['brand_name'] 	=trim($this->input->post("brand_name"));
		$brand['modify_by']	=$_SESSION['user_name'];
		$brand['modify_time']	=date("Y-m-d H:i:s");
		$this->load->library("sys/brand");
		$result =$this->brand->update_brand($brand,$brand_id);
		$this->load->helper("common");
		echo JSON($result);
	}

	public function delete_brand() {
		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_brand",$_SESSION['user_id'])) {

			$this->load->library("common/user_menu");
			$data['user_menu']=$this->user_menu->get_user_menu();
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$this->load->library("sys/brand");
		$brand_id	= intval($this->input->get('brand_id'));
		$this->brand->set_delete_brand($brand_id);
		echo "<script>history.go(-1)</script>";
	}

	public function search_brand(){
		$this->load->library("sys/brand");
		$keyword	= $this->input->get_post("keyword");
		$data['brand'] =$this->brand->search_brand($keyword);
		$this->load->helper("common");
		echo JSON($data);
	}


}